﻿using MvcApplication2.Models;
using MvcApplication2.Models.DTO;
using MvcApplication2.Models.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult DatMua()
        {
            var model = new DatMuaModel();
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.SanPham = SanPhamService.GetByIdSanPham(Convert.ToInt32(Request["idSanPham"]));
            model.DaiLyList = DaiLyService.GetListDaiLy();
            return View(model);
        }

        [HttpPost]
        public ActionResult DatMua(DatMuaModel model)
        {
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.DaiLyList = DaiLyService.GetListDaiLy();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            if (ModelState.IsValid)
            {
                var nguoiDung = Session["NguoiDung"] as NguoiDung;
                var nguoiDungSanPham = new NguoiDungSanPham();
                nguoiDungSanPham.DiemThuong = model.SanPham.DiemThuong ?? 0;
                nguoiDungSanPham.DonGia = model.SanPham.Gia ?? 0;
                nguoiDungSanPham.IdDaiLyNhanSanPham = model.IdDaiLyNhanSanPham ?? 0;
                nguoiDungSanPham.IdNguoiDung = nguoiDung.Id;
                nguoiDungSanPham.IdSanPham = model.SanPham.Id;
                nguoiDungSanPham.IdTrangThaiSanPham = DummyEnum.TrangThaiSanPhamEnum.DaDatMua.GetHashCode();
                nguoiDungSanPham.SoLuong = model.SoLuongDat ?? 0;
                nguoiDungSanPham.TenSanPham = model.SanPham.TenSanPham;
                nguoiDungSanPham.TongDiemThuong = nguoiDungSanPham.DiemThuong*nguoiDungSanPham.SoLuong;
                nguoiDungSanPham.TongGia = nguoiDungSanPham.DonGia*nguoiDungSanPham.SoLuong;
                model.Error = NguoiDungSanPhamService.DatMuaSanPham(nguoiDungSanPham);
            }
            return View(model);
        }

        public ActionResult DoiThuong()
        {
            var model = new DatMuaModel();
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.SanPham = SanPhamService.GetByIdSanPham(Convert.ToInt32(Request["idSanPham"]));
            model.DaiLyList = DaiLyService.GetListDaiLy();
            return View(model);
        }

        [HttpPost]
        public ActionResult DoiThuong(DatMuaModel model)
        {
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.DaiLyList = DaiLyService.GetListDaiLy();
            if (ModelState.IsValid)
            {
                var nguoiDung = Session["NguoiDung"] as NguoiDung;
                var nguoiDungSanPham = new NguoiDungSanPham();
                nguoiDungSanPham.DiemThuong = model.SanPham.DiemNhanThuong ?? 0;
                nguoiDungSanPham.DonGia = model.SanPham.Gia ?? 0;
                nguoiDungSanPham.IdDaiLyNhanSanPham = model.IdDaiLyNhanSanPham ?? 0;
                nguoiDungSanPham.IdNguoiDung = nguoiDung.Id;
                nguoiDungSanPham.IdSanPham = model.SanPham.Id;
                nguoiDungSanPham.IdTrangThaiSanPham = DummyEnum.TrangThaiSanPhamEnum.DaDatMua.GetHashCode();
                nguoiDungSanPham.SoLuong = model.SoLuongDat ?? 0;
                nguoiDungSanPham.TenSanPham = model.SanPham.TenSanPham;
                nguoiDungSanPham.TongDiemThuong = nguoiDungSanPham.DiemThuong * nguoiDungSanPham.SoLuong;
                nguoiDungSanPham.TongGia = nguoiDungSanPham.DonGia * nguoiDungSanPham.SoLuong;
                if(nguoiDungSanPham.TongDiemThuong<=model.TongDiemThuong)
                {
                    model.Error = NguoiDungSanPhamService.DoiThuongSanPham(nguoiDungSanPham);
                    model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
                }
                else
                {
                    model.Error = "Không thể đổi thưởng vì điểm thưởng còn lại không đủ";
                }
            }
            return View(model);
        }

        public ActionResult XemSanPhamDatMua()
        {
            var model = new XemSanPhamDatModel();
            if(!string.IsNullOrEmpty(Request.Params["message"]))
            {
                model.Error = Request.Params["message"];
            }
            var nguoiDung = Session["NguoiDung"] as NguoiDung;
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.NguoiDungSanPhamList = NguoiDungSanPhamService.LayDanhSachByIdTaiKhoan(nguoiDung.Id,0);
            return View(model);
        }

        public ActionResult HuyDatMua()
        {
            string mesage = NguoiDungSanPhamService.HuyDatMua(Convert.ToInt32(Request.Params["idNguoiDungSanPham"]),
                                                              Convert.ToInt32(Request.Params["idSanPham"]),
                                                              Convert.ToInt32(Request.Params["soLuong"]));
            return RedirectToAction("XemSanPhamDatMua",new {message=mesage});
        }

        public ActionResult CapNhatDatMua()
        {
            var model = new CapNhatDatMuaModel();
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.DaiLyList = DaiLyService.GetListDaiLy();
            model.NguoiDungSanPham =
                NguoiDungSanPhamService.GetById(Convert.ToInt32(Request.Params["idNguoiDungSanPham"]));
            return View(model);
        }

        [HttpPost]
        public ActionResult CapNhatDatMua(CapNhatDatMuaModel model)
        {
            if(ModelState.IsValid)
            {
                model.Error = NguoiDungSanPhamService.CapNhatDatMuaSanPham(model.NguoiDungSanPham.Id,
                                                                           model.NguoiDungSanPham.IdSanPham,
                                                                           model.NguoiDungSanPham.SoLuong,
                                                                           model.NguoiDungSanPham.IdDaiLyNhanSanPham);
            }
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.DaiLyList = DaiLyService.GetListDaiLy();
            return View(model);
        }

        public ActionResult XemSanPhamNhanThuong()
        {
            var model = new XemSanPhamDatModel();
            if (!string.IsNullOrEmpty(Request.Params["message"]))
            {
                model.Error = Request.Params["message"];
            }
            var nguoiDung = Session["NguoiDung"] as NguoiDung;
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.NguoiDungSanPhamList = NguoiDungSanPhamService.LayDanhSachByIdTaiKhoan(nguoiDung.Id, 1);
            return View(model);
        }

        public ActionResult HuyNhanThuong()
        {
            var nguoiDung = Session["NguoiDung"] as NguoiDung;
            string mesage = NguoiDungSanPhamService.HuyNhanThuong(Convert.ToInt32(Request.Params["idNguoiDungSanPham"]),
                                                              Convert.ToInt32(Request.Params["idSanPham"]),
                                                              Convert.ToInt32(Request.Params["soLuong"]), nguoiDung.Id, Convert.ToInt32(Request.Params["tongDiemThuong"]));
            return RedirectToAction("XemSanPhamNhanThuong", new { message = mesage });
        }

        public ActionResult XacNhanHang()
        {
            var model = new XacNhanHangModel();
            if(!string.IsNullOrEmpty(Request["idNguoiDungSanPham"]))
            {
                var nguoiDung = Session["NguoiDung"] as NguoiDung;
                model.Username = Request["username"];
                model.IdTrangThaiSanPham = Convert.ToInt32(Request["idTrangThaiSanPham"]);
                model.IsPhanThuong = Convert.ToInt32(Request["isPhanThuong"]) == 1 ? true : false;
                model.Error = NguoiDungSanPhamService.XacNhanHang(Convert.ToInt32(Request["idNguoiDungSanPham"]),
                                                                  Convert.ToInt32(Request["idSanPham"]),
                                                                  Convert.ToInt32(Request["idNguoiDung"]),
                                                                  Convert.ToInt32(Request["isPhanThuong"]));
                model.NguoiDungSanPhamList = NguoiDungSanPhamService.LayDanhSachXacNhanHang(model.Username,
                                                                                            model.IdTrangThaiSanPham,
                                                                                            Convert.ToInt32(Request["isPhanThuong"]),
                                                                                            nguoiDung.
                                                                                                IDDaiLy ?? 0);
            }
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.TrangThaiSanPhamList = TrangThaiSanPhamService.LayDanhSachTrangThaiSanPham();
            return View(model);
        }

        [HttpPost]
        public ActionResult XacNhanHang(XacNhanHangModel model)
        {
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            model.TrangThaiSanPhamList = TrangThaiSanPhamService.LayDanhSachTrangThaiSanPham();
            if(ModelState.IsValid)
            {
                var nguoiDung = Session["NguoiDung"] as NguoiDung;
                model.NguoiDungSanPhamList = NguoiDungSanPhamService.LayDanhSachXacNhanHang(model.Username,
                                                                                            model.IdTrangThaiSanPham,
                                                                                            model.IsPhanThuong?1:0,
                                                                                            nguoiDung.
                                                                                                IDDaiLy??0);
            }
            return View(model);
        }

        public ActionResult ThemSanPham()
        {
            var model = new ThemSanPhamModel();
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            return View(model);
        }

        [HttpPost]
        public ActionResult ThemSanPham(ThemSanPhamModel model)
        {
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            if(ModelState.IsValid)
            {
                var file = Request.Files[0] as HttpPostedFileBase;
                model.Error = SanPhamService.ThemSanPham(model.SanPham, file, Request.PhysicalApplicationPath + "Content/Images/");
            }
            return View(model);
        }
    }
}
